<template>
  <div class="mod-pickAddr">
    <avue-crud ref="crudRef" :page="page" :data="dataList" :option="tableOption" :permission="permission"
      @search-change="onSearch" @selection-change="selectionChange" @on-load="getDataList">
      <template #menu-left>
        <n-button strong secondary type="info" round v-if="isAuth('shop:pickAddr:save')" @click.stop="onAddOrUpdate()">
          新增
        </n-button>

        <n-button strong secondary type="error" round v-if="isAuth('shop:pickAddr:delete')"
          :disabled="dataListSelections.length <= 0" @click="onDelete()">
          批量删除
        </n-button>
      </template>

      <template #menu="scope">
        <n-button strong secondary type="info" round v-if="isAuth('shop:pickAddr:update')" icon="el-icon-edit"
          @click.stop="onAddOrUpdate(scope.row.addrId)">
          编辑
        </n-button>

        <n-button strong secondary type="error" round v-if="isAuth('shop:pickAddr:delete')" icon="el-icon-delete"
          @click.stop="onDelete(scope.row.addrId)">
          删除
        </n-button>
      </template>
    </avue-crud>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdateRef" @refresh-data-list="getDataList" />
  </div>
</template>

<script setup>
import { isAuth } from '@/utils'

import AddOrUpdate from './add-or-update.vue'
import { tableOption } from '@/crud/shop/pickAddr.js'

import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { createVNode } from 'vue';
import { Modal } from 'ant-design-vue';
import { notification } from 'ant-design-vue';
const permission = {
  delBtn: isAuth('prod:prod:delete')
}
const dataList = ref([])
const dataListSelections = ref([])
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
/**
 * 获取数据列表
 */
const getDataList = (pageParam, params, done) => {
  http({
    url: http.adornUrl('/shop/pickAddr/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.pageSize : pageParam.pageSize
        },
        params
      )
    )
  })
    .then(({ data }) => {
      dataList.value = data.records
      page.total = data.total
      if (done) done()
    })
}

const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref(null)
/**
 * 新增 / 修改
 * @param id
 */
const onAddOrUpdate = (id) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(id)
  })
}

/**
 * 删除
 * @param id
 */
const onDelete = (id) => {
  const ids = id ? [id] : dataListSelections.value?.map(item => {
    return item.addrId
  })






  Modal.confirm({
    title: `确定进行[${id ? '删除' : '批量删除'}]操作?`,
    icon: createVNode(ExclamationCircleOutlined),
    content: '请确认是否进行删除',
    onOk: async () => {  // 使用async函数来确保返回一个Promise  
      try {
        await http({  // 确保http函数返回Promise  
          url: http.adornUrl('/shop/pickAddr'),
          method: 'delete',
          data: http.adornData(ids, false),
        });

        notification.open({
          message: '操作成功',
          description:
            '操作成功',

        });

        getDataList(); // 刷新数据列表  
      } catch (error) {
        notification.open({
          message: '操作失败',
          description:
            '操作失败',

        });

      }
    },
    onCancel() { },
  });
}

/**
 * 条件查询
 * @param params
 * @param done
 */
const onSearch = (params, done) => {
  getDataList(page, params, done)
}
/**
 * 多选变化
 * @param val
 */
const selectionChange = (val) => {
  dataListSelections.value = val
}
</script>
